﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace updateMain
{
    public partial class Download : Form
    {
        public Download()
        {
            InitializeComponent();
        }
        private void form_show(object sender, EventArgs e)
        {
            var url = "http://172.16.8.67:8090/version.xml";
            UpdateDll.DownLoad(url, (string data, string code, string result) => {
                Console.WriteLine(result + "====res==========");
                JObject value = JObject.Parse(result);
                if (value["success"] != null && "true".Equals(value["success"].ToString().ToLower()))
                {
                    MessageBox.Show("下载完成，开始更新");
                    UpdateDll.Update("-wait 5", (string updateCode, string updateStr, string updateResult) =>
                    {
                        Console.WriteLine(updateResult);
                    }, "update");
                }
                else if(value["file"] != null)
                {
                    Console.WriteLine(data + "====data==========");
                    var file = value["file"].ToString();
                    int end = file.LastIndexOf("/");
                    file = file.Substring(end+1);
                    Console.WriteLine(file);
                    label1.Text+= "正在下载：" + file;
                }
            }, "download");
        }
        private void Download_Load(object sender, EventArgs e)
        {
            
        }
        private static System.Diagnostics.Process p;
        private void Button1_Click(object sender, EventArgs e)
        {
            if (p == null)
            {
                p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "testUpdate.exe";
                p.Start();
            }
            else
            {
                if (p.HasExited) //是否正在运行
                {
                    p.Start();
                }
            }
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        }

        private void ProgressBar1_Click(object sender, EventArgs e)
        {

        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }
    }
}
